package com.tanhua.autoconfig.templates;

import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import com.tanhua.autoconfig.propertirs.SmsProperties;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

public class SmsTemplate {
    @Autowired
    private SmsProperties smsProperties;
    public void sendMsg(String mobile,String code){
            //签名ID。（联系客服申请。测试请用：2e65b1bb3d054466b82f0c9d125465e2）
            String smsSignId=smsProperties.getSmsSignId();
            //模板ID。（联系客服申请。测试ID请用：908e94ccf08b4476ba6c876d13f084ad，短信内容为 { 验证码：**code**，**minute**分钟内有效，请勿泄漏于他人！}）
            String templateId=smsProperties.getTemplateId();
            //应用code  https://market.console.aliyun.com/imageconsole/index.htm?#/bizlist?_k=r5f9m0 查找
            String appCode=smsProperties.getAppcode();


            //请求连接
            String host = "https://gyytz.market.alicloudapi.com/sms/smsSend";
            //拼装请求体
            Map<String, Object> querys = new HashMap<String, Object>();
            querys.put("mobile", mobile);
            querys.put("param", "**code**:"+code+",**minute**:5");
            querys.put("smsSignId", smsSignId);
            querys.put("templateId", templateId);

            try {
                String result = HttpRequest.post(host)
                        .header(Header.AUTHORIZATION, "APPCODE " + appCode)//头信息，多个头信息多次调用此方法即可
                        .form(querys)//表单内容
                        .timeout(20000)//超时，毫秒
                        .execute().body();
                System.out.println(result);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

